<template>
  <div>
    <el-dialog
      v-if="fileDialogVisible"
      title="文件附件替换"
      :visible.sync="fileDialogVisible"
      width="60%"
    >
      <div class="tableList">
        <el-table
          :data="tableData"
          style="width: 100%"
          border
        >
          <el-table-column
            prop="fileName"
            label="名称"
            align="center"
          />
          <el-table-column
            prop="updateTime"
            label="上传时间"
            align="center"
          />
          <el-table-column
            prop="updateBy"
            label="上传人员"
            align="center"
          />
          <el-table-column
            align="center"
            label="操作"
          >
            <template slot-scope="scope">
              <el-upload
                ref="upload"
                :on-change="handleUploadChange"
                action=""
                :file-list="fileList"
                :show-file-list="false"
                :auto-upload="false"
                :with-credentials="true"
                :accept="'.doc,.pdf'"
                :http-request="submitFile"
              >
                <span style="color:#409EFF;cursor:pointer" @click="replaceFile(scope.row,scope.$index)">替换文件</span>
              </el-upload>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button plain @click="confirm"><i class="el-icon-close" />  关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
    name: 'ReplaceFile',
    components: {

    },
    props: {
        attachments: { type: Array, default: function() { return [] } },
        bodyfile: { type: Object, default: function() { return {} } },
        wfInstance: { type: Object, default: function() { return {} } },
        vo: { type: Object, default: function() { return {} } },
        wfAttachUrl: { type: String, default: '' },
        updateAttachList: { type: Function, default: function() { return true } }
    },
    data() {
        return {
            tableData: [],
            fileDialogVisible: false,
            oldFile: {},
            tbindex: ''
        }
    },
    computed: {

    },
    watch: {

    },
    created() {
        this.getData()
        this.$bus.on('clickReplaceFile', this.clickReplaceFile)
    },
    beforeDestroy() {
        this.$bus.off('clickReplaceFile', this.clickReplaceFile)
    },
    mounted() {

    },
    methods: {
        confirm() {
            this.fileDialogVisible = false
        },
        clickReplaceFile() {
            this.fileDialogVisible = true
        },
        async handleUploadChange(file, fileList) {
            const oldFile = this.oldFile.fileName
            const newFile = file.name
            const oldName = oldFile.substr(oldFile.lastIndexOf('.') + 1)
            const newName = newFile.substr(newFile.lastIndexOf('.') + 1)

            if (oldName !== newName) {
                this.$message.warning('请替换相同后缀名的文件！')
            } else {
                let val = ''
                if (newName === 'pdf') {
                    val = 'BODY'
                } else {
                    val = 'ATTACH'
                }

                var reqData = { module: this.wfInstance.module, busiDataId: this.vo.id, attachsType: val, attachsSize: file.size, bodyDocId: this.wfInstance.bodyDocId, clientType: 'uploader' }
                const uploadRst = await this.$wfEngine.upload(this, this.wfAttachUrl + '/upload', file.raw, reqData)
                if (uploadRst.code === 'success') {
                    this.$message.success('替换成功')
                    if (typeof this.updateAttachList === 'function') {
                        this.updateAttachList(uploadRst.body.uploadAttachList[0])
                    }
                    this.tableData[this.tbindex] = uploadRst.body.uploadAttachList[0]
                }
            }
        },
        replaceFile(val, i) {
            this.oldFile = val
            this.tbindex = i
        },
        getData() {
            this.tableData.push(this.bodyfile)

            this.attachments.forEach(item => {
                if (item.fileType === 'ATTACH') {
                    this.tableData.push(item)
                }
            })
        }
    }
}
</script>

<style scoped lang="scss">

</style>
